package com.jinlia.show.modules.system.utils;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.StringUtils;

import java.util.List;

public class EasyExcelWarp {


    public static CellRangeAddressWarp isInMergeRange(List<CellRangeAddressWarp> list, Cell cell) {
        if (cell == null) {
            return null;
        }
        for (CellRangeAddressWarp cellRangeAddressWarp : list) {
            if (cellRangeAddressWarp.cellAddresses.isInRange(cell)) {
                if (StringUtils.isEmpty(cellRangeAddressWarp.getCellValue())) {
                    String cellValue = getCellValueAsString(cell);
                    cellRangeAddressWarp.setCellValue(cellValue);
                }
                return cellRangeAddressWarp;
            }

        }
        return null;
    }

    public static String getCellValueAsString(Cell cell) {
        String cellValue = "";
        switch (cell.getCellType()) {
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA:
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            default:
                cellValue = "";
                break;
        }
        return cellValue;
    }

    public static class CellRangeAddressWarp {
        CellRangeAddress cellAddresses;

        String cellValue;
        int firstRow;
        int lastRow;
        int firstCol;
        int lastCol;

        public CellRangeAddressWarp(CellRangeAddress cellAddresses) {
            this.cellAddresses = cellAddresses;
            this.firstRow = cellAddresses.getFirstRow();
            this.lastRow = cellAddresses.getLastRow();
            this.firstCol = cellAddresses.getFirstColumn();
            this.lastCol = cellAddresses.getLastColumn();
        }

        public CellRangeAddress getCellAddresses() {
            return cellAddresses;
        }

        public void setCellAddresses(CellRangeAddress cellAddresses) {
            this.cellAddresses = cellAddresses;
        }

        public String getCellValue() {
            return cellValue;
        }

        public void setCellValue(String cellValue) {
            this.cellValue = cellValue;
        }

        public int getFirstRow() {
            return firstRow;
        }

        public void setFirstRow(int firstRow) {
            this.firstRow = firstRow;
        }

        public int getLastRow() {
            return lastRow;
        }

        public void setLastRow(int lastRow) {
            this.lastRow = lastRow;
        }

        public int getFirstCol() {
            return firstCol;
        }

        public void setFirstCol(int firstCol) {
            this.firstCol = firstCol;
        }

        public int getLastCol() {
            return lastCol;
        }

        public void setLastCol(int lastCol) {
            this.lastCol = lastCol;
        }
    }
}
